#pragma once
#include "RayTracing.h"

#define pi 3.14159265359

GLint screenWidth = 640, screenHeight = 640;

Draw_Scene *hc;
RayTrace *rt;

Camera cam(Vector(0,0,15),Vector(0,0,0)-Vector(0,0,15),Vector(0,0,0),Vector(0,0,0));
Ray ray(cam.cam_pos,cam.cam_direction);
Light light (Vector(0,-4,0),Vector(0,1,0)-Vector(0,-4,0),Color(1,1,1,0.3),5.0);
GLUI *glui;
Plane ground(Vector(2,2,8),Vector(-2,2,8),Vector(-2,2,-5),Vector(2,2,-5),Color(0,25,0,255),7,"ground");
Plane back_wall(Vector(-2,2,-5),Vector(2,2,-5),Vector(2,-5,-1.8),Vector(-2,-5,-1.8),Color(0,50,0,255),7,"back_wall");
Plane left_wall(Vector(-2,2,8),Vector(-2,2,-5),Vector(-2,-5,-1.8),Vector(-2,-3.9,8),Color(0,50,0,255),7,"left_wall");
Plane right_wall(Vector(2,2,8),Vector(2,2,-5),Vector(2,-5,-1.8),Vector(2,-3.9,8),Color(0,50,0,255),7,"right_wall");
Plane p(Vector(1,1,3),Vector(-1,1,3),Vector(-1,1,0),Vector(1,1,0),Color(10,50,10,255),7,"table");

Pixel *pixel = new Pixel[640*640];

//Draw Method
void Draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 	

	glPushMatrix();

	vector<Vector> intersections;
	vector<Plane*> planes;
	planes.push_back(&p);
	planes.push_back(&ground);
	planes.push_back(&back_wall);
	planes.push_back(&left_wall);
	planes.push_back(&right_wall);
	//CROSS PRODUCT OF TWO VECTORS THAN NORMALIZE
	p.normal = ((p.plane_v1-p.plane_v2)^(p.plane_v2-p.plane_v3)).normalize();	
	ground.normal = ((ground.plane_v1-ground.plane_v2)^(ground.plane_v2-ground.plane_v3)).normalize();
	back_wall.normal = ((back_wall.plane_v1-back_wall.plane_v2)^(back_wall.plane_v2-back_wall.plane_v3)).normalize();
	left_wall.normal = ((left_wall.plane_v1-left_wall.plane_v2)^(left_wall.plane_v2-left_wall.plane_v3)).normalize().invert(); 
	right_wall.normal = ((right_wall.plane_v1-right_wall.plane_v2)^(right_wall.plane_v2-right_wall.plane_v3)).normalize();

	//Setup Indirect Light Ray Position on the table..
	Ray to_table_ray;
	to_table_ray.origin = light.position;
	to_table_ray.Direction = light.Direction;
	planes.at(0)->findIntersection(to_table_ray);
	to_table_ray.origin = planes.at(0)->intersection;

	float invWidth = 1 / (float)(screenWidth), invHeight = 1 / (float)(screenHeight);
	float fov = 30, aspectratio = screenWidth / (float)(screenHeight);
	float angle = tan(pi * 0.5 * fov / (float)(180));
	// Trace rays
	int v=0;
	for (unsigned y = 0; y < screenHeight; ++y) {
		for (unsigned x = 0; x < screenWidth; ++x, ++v) {
			float xx =(2 * ((x + 0.5) * invWidth) - 1) * angle * aspectratio;
			float yy = (1 - 2 * ((y + 0.5) * invHeight)) * angle;
			Vector raydir(xx, yy, -1);
			raydir.normalize();
			Ray r(cam.cam_pos,raydir);
			pixel[v] = rt->r_trace(r, planes,light,to_table_ray);
		}
	}
		glDrawPixels(screenWidth,screenHeight,GL_RGBA,GL_UNSIGNED_BYTE,pixel);

	glPopMatrix();
	glFlush();
	glutSwapBuffers();
}

//Constructor
Draw_Scene::Draw_Scene()
{
	glui = GLUI_Master.create_glui( "Options", 0,screenWidth+50,0 );
}

/**
Main method, setup our window
*/
int main(int iargc, char** argv){
	int window_id;
	glutInit(&iargc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
	glutInitWindowPosition(0, 0);
	glutInitWindowSize(screenWidth, screenHeight);
	window_id = glutCreateWindow("Shadow Rendering");
	glutDisplayFunc(Draw);
	hc = new Draw_Scene();
	glui->set_main_gfx_window( window_id );
	glutMainLoop();
}